import 'package:flutter/material.dart';
import '../../helper/utils/util.dart';

class CarouselStatusText extends StatefulWidget {
  const CarouselStatusText({
    Key? key,
    required this.list,
    this.width,
    required this.height,
    this.lineCount = 3,
    this.onTap,
    this.color,
  }) : super(key: key);
  final List<String> list;
  final double height;
  final double? width;
  final int lineCount;
  final Function(int)? onTap;
  final Color? color;

  @override
  State<CarouselStatusText> createState() => _CarouselTextState();
}

class _CarouselTextState extends State<CarouselStatusText> {
  final ctrl = ScrollController();
  int currentIndex = 0;
  late double lineHeight;
  late List<String> fixlist;

  @override
  void initState() {
    super.initState();
    lineHeight = widget.height / widget.lineCount;

    fixList();
    Future.delayed(const Duration(seconds: 5)).then((value) {
      run();
    });
  }

  void fixList() {
    var _list = widget.list;
    var _fixCount = _list.length % widget.lineCount;
    if (_fixCount == 0) {
      _fixCount = widget.lineCount;
    }
    fixlist = _list;
    var _suplements = _list.sublist(0, _fixCount);
    fixlist.addAll(_suplements);
  }

  void run() {
    if (ctrl.positions.isEmpty) {
      return;
    }
    ++currentIndex;

    ctrl.animateTo(
      currentIndex * lineHeight,
      duration: const Duration(milliseconds: 800),
      curve: Curves.easeInOut,
    );
    if (currentIndex >= fixlist.length - widget.lineCount) {
      currentIndex = 0;
      Future.delayed(const Duration(seconds: 1)).then((value) {
        ctrl.jumpTo(currentIndex * lineHeight);
      });
    }
    Future.delayed(const Duration(seconds: 3)).then((value) {
      run();
    });
  }

  @override
  void dispose() {
    super.dispose();
    ctrl.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10.r),
      height: widget.height,
      width: widget.width,
      child: ListView.builder(
        physics: const NeverScrollableScrollPhysics(),
        controller: ctrl,
        itemCount: fixlist.length,
        itemBuilder: (context, index) {
          var _item = widget.list[index];
          return InkWell(
            onTap: () {
              if (widget.onTap != null) {
                widget.onTap!(index);
              }
            },
            child: Row(
              children: [
                Container(
                  width: 4.r,
                  height: 4.r,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(180),
                      color: const Color(0xffC0C6D3)),
                  margin: EdgeInsets.only(left: 15.r, right: 10.r),
                ),
                Expanded(
                    child: Container(
                  height: lineHeight,
                  alignment: Alignment.centerLeft,
                  margin: EdgeInsets.only(right: 10.r),
                  child: Text(
                    _item,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        color: widget.color ?? Colors.white, fontSize: 12.r),
                  ),
                )),
                Image.asset(
                  'icon_tag4.png'.imgPathHome,
                  width: 50.r,
                  height: 20.r,
                )
              ],
            ),
          );
        },
      ),
    );
  }
}
